package com.smarth.zhizulian.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.activity.AboutUsActivity;
import com.smarth.zhizulian.activity.AgentWebActivity;
import com.smarth.zhizulian.activity.FavoriteActivity;
import com.smarth.zhizulian.activity.LoginActivity;
import com.smarth.zhizulian.activity.MessageCenterActivity;
import com.smarth.zhizulian.activity.MissionActivity;
import com.smarth.zhizulian.activity.MoneyDetailActivity;
import com.smarth.zhizulian.activity.PersonalInfoActivity;
import com.smarth.zhizulian.activity.SettingsActivity;
import com.smarth.zhizulian.activity.ShareMeActivity;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.EventFragment;
import com.smarth.zhizulian.bean.user.MineIndex;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.entity.UserInfo;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.CommonUtils;
import com.smarth.zhizulian.utils.GlideUtils;
import com.smarth.zhizulian.utils.UserUtils;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.impl.adapter.PageDataAdapter;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpPageDataModel;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.Observable;

/**
 * Created by 18865 on 2017/12/21.
 */

public class MineFragment extends EventFragment {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_avatar)
    CircleImageView ivAvatar;
    @BindView(R.id.iv_medal)
    ImageView ivMedal;
    @BindView(R.id.tv_coin)
    TextView tvCoin;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.rl_header_login)
    RelativeLayout rlHeaderLogin;
    @BindView(R.id.ll_header_unlogin)
    LinearLayout llHeaderUnlogin;
    @BindView(R.id.rv_refresh)
    SmartRefreshLayout rvRefresh;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.tv_score)
    TextView tvScore;
    private RefreshHelper<MineIndex> refreshHelper;
    private PageDataAdapter<MineIndex> pageDataAdapter;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.fragment_mine);
        init();
    }

    private void init() {
        if (UserUtils.isLogin()) {
            rvRefresh.setEnableRefresh(true);
            rlHeaderLogin.setVisibility(View.VISIBLE);
            llHeaderUnlogin.setVisibility(View.GONE);
            refreshHelper = new RefreshHelper<>(mContext, llLoadinglayout, rvRefresh);
            refreshHelper.setDataModel(new HttpPageDataModel<MineIndex>(mContext) {
                @Override
                public Observable<MineIndex> getObservable() {
                    return KumaHttp.getService().mineIndex()
                            .compose(RxCompos.<MineIndex>getBELoginCompos());
                }
            });
            pageDataAdapter = new PageDataAdapter<MineIndex>() {
                @Override
                public void onGetData(MineIndex mineIndex, boolean isRefresh) {
                    super.onGetData(mineIndex, isRefresh);
                    tvName.setText(mineIndex.getNickName());
                    GlideUtils.loadAvatar(mContext, mineIndex.getAvatar(), ivAvatar);
                    tvCoin.setText("" + mineIndex.getCoin());
                    tvMoney.setText(CommonUtils.formatMoney(mineIndex.getCoin() / 1000d) + "元");
                    switch (mineIndex.getLevel()) {
                        case 0:
                            ivMedal.setImageResource(R.drawable.gerenzhongxin_ordinary);
                            break;
                        case 1:
                            ivMedal.setImageResource(R.drawable.gerenzhongxin_copper);
                            break;
                        case 2:
                            ivMedal.setImageResource(R.drawable.medal_silver);
                            break;
                        case 3:
                            ivMedal.setImageResource(R.drawable.gerenzhongxin_gold);
                            break;
                    }
                    ivMessage.setSelected(mineIndex.getIsMessageApp() == 1 || mineIndex.getIsMessageNotice() == 1);
                    EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.ON_GET_MINE, "MineFragment", mineIndex), "MainActivity");
                    tvScore.setText("当前分数" + mineIndex.getScore());
                }
            };
            refreshHelper.setDataAdapter(pageDataAdapter);
            refreshHelper.refresh();
        } else {
            rvRefresh.setEnableRefresh(false);
            rlHeaderLogin.setVisibility(View.GONE);
            llHeaderUnlogin.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onLogin(EventBean eb) {
        init();
    }

    @OnClick({R.id.iv_avatar, R.id.iv_settings, R.id.tv_login, R.id.iv_wallet, R.id.iv_shoucang, R.id.iv_sign, R.id.iv_us, R.id.iv_invite, R.id.rl_mission, R.id.rl_draw, R.id.rl_show, R.id.rl_qa, R.id.iv_message, R.id.rl_rank})
    public void onViewClicked(View view) {
        if (!UserUtils.isLogin()) {
            LoginActivity.open(mContext);
            return;
        }
        switch (view.getId()) {
            case R.id.iv_avatar:
                PersonalInfoActivity.open(mContext);
                break;
            case R.id.iv_settings:
                SettingsActivity.open(mContext);
//                KumaDialogFactory.buildActivityResultDialog(mContext, "" + 2, "" + 300).show();
                break;
            case R.id.tv_login:
                break;
            case R.id.iv_wallet:
                MoneyDetailActivity.open(mContext);
                break;
            case R.id.iv_shoucang:
                FavoriteActivity.open(mContext);
                break;
            case R.id.rl_qa:
                AgentWebActivity.openForQuestion(mContext);
                break;
            case R.id.iv_sign:
                MissionActivity.open(mContext);
                break;
            case R.id.iv_us:
                AboutUsActivity.open(mContext);
                break;
            case R.id.iv_invite:
                EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.SHOW_INVITEDICIPLE), "MainActivity");
                EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.SHOW_INVITEDICIPLE), "InviteFragment");
                break;
            case R.id.rl_mission:
                MissionActivity.open(mContext);
                break;
            case R.id.rl_draw:
                AgentWebActivity.openForDraw(mContext);
                break;
            case R.id.rl_show:
                UserInfo ui = new UserInfo();
                ui.setName(pageDataAdapter.getMyData().getNickName());
                ui.setQrcode(pageDataAdapter.getMyData().getQrCodeImageUrl());
                ui.setCoin(pageDataAdapter.getMyData().getHistoryCoin());
                ui.setAvatar(pageDataAdapter.getMyData().getAvatar());
                ui.setWechatUrl(pageDataAdapter.getMyData().getWxShareUrl());
                ShareMeActivity.open(mContext, ui);
                break;
            case R.id.iv_message:
                MessageCenterActivity.openRed(mContext, pageDataAdapter.getMyData().getIsMessageNotice() == 1);
                break;
            case R.id.rl_rank:
                AgentWebActivity.openForMember(mContext);
                break;
        }
    }

    @Subscriber(tag = BaseConfig.EventTag.ON_GETMONEY)
    private void onGetMoney(EventBean eb) {
        refreshHelper.refresh();
    }

    @Subscriber(tag = BaseConfig.EventTag.ON_DEAL_NOTICE)
    private void onEventRefreshMine(EventBean eb) {
        refreshHelper.refresh();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MineFragment");
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MineFragment");
    }
}
